package com.movitech.mobile.cz.base.common.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
@Component
public class WebUtils {

    private static HttpServletRequest request;

    @Autowired
    public WebUtils( HttpServletRequest request) {
        WebUtils.request = request;
    }
    public static  String getIpAddr(){
        String ip = request.getHeader("X-Real-IP");
        try {
            if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
                return ip;
            }
            ip = request.getHeader("X-Forwarded-For");
            if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
                int index = ip.indexOf(',');
                if (index != -1) {
                    return ip.substring(0, index);
                } else {
                    return ip;
                }
            } else {
                return request.getRemoteAddr();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ip;
    }
}
